/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package taini;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import taini.koneksi.KoneksiAini;

/**
 *
 * @author Aini
 */
public class updateTFIDF {
    KoneksiAini theKoneksi = new KoneksiAini();

    public updateTFIDF() {
        try {
            theKoneksi.connectFirst();
        } catch (SQLException ex) {
            Logger.getLogger(updateTFIDF.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


     public void updateIDF() {
        String getIdTerm = "SELECT id_term FROM terms ";
        String getCountDok = "SELECT COUNT(id_dok) FROM dokumen";
        int countDok = 1;
        LinkedList<Integer> listId = new LinkedList<Integer>();
        ResultSet rs;
        try {
            rs = theKoneksi.executeSelect(getIdTerm);
            while(rs.next())
            {
                listId.add(rs.getInt(1));
            }
            rs = theKoneksi.executeSelect(getCountDok);
            while(rs.next())
            {
                countDok = rs.getInt(1);
            }
        } catch (SQLException ex) {
            Logger.getLogger(updateTFIDF.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalStateException ex) {
            Logger.getLogger(updateTFIDF.class.getName()).log(Level.SEVERE, null, ex);
        }
        for(int id : listId)
        {
            String getCountDokWithId = "SELECT COUNT(V.ID_DOK) FROM TERMS T, VEKTOR V WHERE T.ID_TERM = V.ID_TERM AND T.ID_TERM = "+id;
            int countDokWithId = 1;
            double idf = 0.0;
            try {
                rs = theKoneksi.executeSelect(getCountDokWithId);
                while(rs.next())
                {
                    countDokWithId = rs.getInt(1);
                }
                if(countDokWithId == 0 ) idf = 0.0;
                else idf= Math.log10((double)countDok/(double)countDokWithId);
            String updateIdfTerm = "UPDATE TERMS SET idf = "+idf+" WHERE ID_TERM ="+id;
                theKoneksi.executeUpdate(updateIdfTerm);
            } catch (SQLException ex) {
                Logger.getLogger(updateTFIDF.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalStateException ex) {
                Logger.getLogger(updateTFIDF.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    public void updateBobot() {
        String getIdDokIdTerm = "SELECT id_dok, id_term FROM VEKTOR ";
        ResultSet rs;
        LinkedList<int[]> listIdDokIdTerm = new LinkedList<int[]>();
        try {
            rs = theKoneksi.executeSelect(getIdDokIdTerm);
            while(rs.next())
            {
                int[] idDokIdTerm = new int[2];
                idDokIdTerm[0] = rs.getInt(1); // mendapatkan id dok
                idDokIdTerm[1] = rs.getInt(2); // mendapatkan id term
                listIdDokIdTerm.add(idDokIdTerm);
            }
        } catch (SQLException ex) {
            Logger.getLogger(updateTFIDF.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalStateException ex) {
            Logger.getLogger(updateTFIDF.class.getName()).log(Level.SEVERE, null, ex);
        }
        for(int[] idDokidTerm : listIdDokIdTerm)
        {
            try {
                String getFreq = "SELECT frekuensi FROM vektor WHERE id_dok ="+idDokidTerm[0]+" AND id_term = "+idDokidTerm[1];
                rs = theKoneksi.executeSelect(getFreq);
                int freq = 0;
                while(rs.next())
                {
                    freq = rs.getInt(1);
                }
                String getIDF = "SELECT idf FROM terms WHERE id_term = "+idDokidTerm[1];
                rs = theKoneksi.executeSelect(getIDF);
                double idf = 0.0;
                while(rs.next())
                {
                    idf = rs.getDouble(1);
                }
                double bobot = (double)freq * idf;
                String updateBobok = "UPDATE VEKTOR set bobot = "+bobot+" WHERE id_dok = "+idDokidTerm[0]+" AND id_term ="+idDokidTerm[1];
                theKoneksi.executeUpdate(updateBobok);
            } catch (SQLException ex) {
                Logger.getLogger(updateTFIDF.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalStateException ex) {
                Logger.getLogger(updateTFIDF.class.getName()).log(Level.SEVERE, null, ex);
            }

        }


      }
}
